package com.ace.slide.data;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.hibernate.Session;

public abstract class BaseDAO
{

    protected static Session session;

    public static void setSession(Session session)
    {
        BaseDAO.session = session;
    }

    public BaseDAO()
    {
    }

    @SuppressWarnings("unchecked")
    protected static <T> List<T> getAll(Class<T> C)
    {
        List<T> result = session.createCriteria(C).list();
        List<T> list = new ArrayList<T>();
        for (ListIterator<T> i = result.listIterator(); i.hasNext();)
        {
            list.add(i.next());
        }
        return list;
    }

    @SuppressWarnings("unchecked")
    protected static <T> T getById(Class<T> C, long id)
    {
        return (T) session.get(C, id);
    }

    @SuppressWarnings("unchecked")
    protected static <T> List<T> GetSql(String sql, Class<T> C)
    {
        List<T> list = session.createSQLQuery(sql).addEntity(C).list();
        return list;
    }

    @SuppressWarnings("unchecked")
    protected static <T> List<T> GetHql(String hql, Class<T> C)
    {
        List<T> list = session.createQuery(hql).list();
        return list;
    }

    public static void save(Object o)
    {
        session.saveOrUpdate(o);
    }

    public static void delete(Object o)
    {
        session.delete(o);
    }

}
